const { warn, error, clean, success } = require('../../core/common')
const mkdirp = require('mkdirp')
const settings = require('../../../settings')

class BaseManager {
  // 配置（名称、存档路径等）
  gameOption = null
  // 游戏名称
  gameName = ''
  // git 仓库 url
  repoPath = ''
  // 游戏存档目录路径
  saveDataDir = null
  // 游戏存档本地目录名称
  dirName = ''

  constructor(gameName) {
    this.gameName = gameName
    this.loadOption(gameName)
  }

  /**
   * 基于游戏名称获取配置
   * @param gameName
   */
  loadOption(gameName) {
    const gameOption = (this.gameOption = settings[gameName] || null)
    if (!gameOption) {
      warn('gameName is not valid.')
      process.exit(-1)
    }

    // 兼容旧版本配置文件，新版配置文件直接使用 dirs
    if (Array.isArray(gameOption.dirs)) {
      this.saveDataDir = gameOption.dirs[0]
    } else if (typeof gameOption.dirs === 'string') {
      this.saveDataDir = gameOption
    }

    // 如果目录不存在则创建下
    mkdirp.sync(this.saveDataDir)
    if (!this.saveDataDir) {
      warn(`No save data dir for game ${gameName}`)
      process.exit(-1)
    }

    this.repoPath = gameOption.repoPath
    this.dirName = gameOption.dirName
  }
}

module.exports = BaseManager
